package com.boot.web.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description Http工具类
 * @date 2019/8/28 17:19
 **/

public class HttpUtil {
    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 获取 HttpServletRequest
     * @date 2019/12/4 20:46
     **/
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 获取 HttpServletResponse
     * @date 2019/12/4 20:46
     **/
    public static HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse();
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 将 ServletRequest 转化为 HttpServletRequest
     * @date 2019/12/4 20:46
     **/
    public static HttpServletRequest toHttp(ServletRequest request) {
        return (HttpServletRequest) request;
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 将 ServletResponse 转化为 HttpServletResponse
     * @date 2019/12/4 20:47
     **/
    public static HttpServletResponse toHttp(ServletResponse response) {
        return (HttpServletResponse) response;
    }

}
